//属性
var interface={
    data:{
        list:[]
    }
}
var interface_add={
    dictionary:[]
}
var interface_edit={
    dictionary:[],
    data:{}
}

//事件
interface.Show=function(){
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/interfacer.php?p=list&s_project_path="+index.rootpath+system.data.s_project_path
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            interface.data.list=res.list;
            m.render(document.querySelector(".content"),m(interface));
        }else{
            msgbox.Error(res.message);
        }
    })
}

interface.Print=function(){
    // index.ShowPrint(system.data.s_project_name+"接口",document.querySelector(".pdfs").outerHTML);
    window.onbeforeprint=function(){
        index.ShowPrint(system.data.s_project_name+"接口",document.querySelector(".pdfs").outerHTML);
    }
    window.onafterprint=function(){
        index.HidePrint();
    }
    window.print();
}

interface.Save=function(){
    var postData=new FormData();
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/interfacer.php?p=save",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            msgbox.Info("写入成功");
        }else{
            msgbox.Error(res.message);
        }
    })
}

interface.SaveCode=function(){
    var postData=new FormData();
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/interfacer.php?p=savecode",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            msgbox.Info("写入成功");
        }else{
            msgbox.Error(res.message);
        }
    })
}

interface_add.Show=function(){
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/dictionary.php?p=list"
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            interface_add.dictionary=res.list;
            m.render(document.querySelector(".content"),m(interface_add));
        }else{
            msgbox.Error(res.message);
        }
    })
}

interface_add.AddRow=function(){
    var tbl=this.parentNode.parentNode.parentNode;
    var tr=tbl.insertRow();
    td1=tr.insertCell();
    td2=tr.insertCell();
    td3=tr.insertCell();
    td4=tr.insertCell();
    td5=tr.insertCell();
    td1.innerHTML='<input type="text" list="dic" onblur="interface_add.GetDictionary(this)" />';
    td2.innerHTML='<input type="text" />';
    td3.innerHTML='<input type="text" />';
    td4.innerHTML='<select><option value="是">是</option><option value="否">否</option></select>';
    td5.innerHTML='<button onclick="interface_add.DelRow(this)">删除</button>';
}
interface_add.DelRow=function(e){
    e.parentNode.parentNode.remove();
}
interface_add.GetDictionary=function(e){
    var tr=e.parentNode.parentNode;
    for (let i = 0; i < interface_add.dictionary.length; i++) {
        if(e.value==interface_add.dictionary[i].id){
            tr.querySelectorAll("input")[0].value=interface_add.dictionary[i].s_en;
            tr.querySelectorAll("input")[1].value=interface_add.dictionary[i].s_cn;
            tr.querySelectorAll("input")[2].value=interface_add.dictionary[i].s_regex;
        }
    }
    for (let i = 0; i < interface_edit.dictionary.length; i++) {
        if(e.value==interface_edit.dictionary[i].id){
            tr.querySelectorAll("input")[0].value=interface_edit.dictionary[i].s_en;
            tr.querySelectorAll("input")[1].value=interface_edit.dictionary[i].s_cn;
            tr.querySelectorAll("input")[2].value=interface_edit.dictionary[i].s_regex;
        }
    }
}
interface_add.Submit=function(){
    if(!/^.{1,300}$/.test(document.querySelector("#s_url"))){
        msgbox.Error("URL格式错误");
        return;
    }
    if(!/^.{1,300}$/.test(document.querySelector("#s_name"))){
        msgbox.Error("接口名称格式错误");
        return;
    }
    var postData=new FormData();
    postData.append("s_method",document.querySelector("#s_method").value);
    postData.append("s_name",document.querySelector("#s_name").value);
    postData.append("s_url",document.querySelector("#s_url").value);
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    var tbl1=document.querySelector(".page_content").querySelectorAll("table")[0];
    var tbl2=document.querySelector(".page_content").querySelectorAll("table")[1];
    for(var i=1;i<tbl1.rows.length;i++){
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[0].value)){
            msgbox.Error("第"+i+"行，参数格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[1].value)){
            msgbox.Error("第"+i+"行，中文描述格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[2].value)){
            msgbox.Error("第"+i+"行，正则格式错误");
        return;
        }
        postData.append("request_en[]",tbl1.rows[i].querySelectorAll("input")[0].value);
        postData.append("request_cn[]",tbl1.rows[i].querySelectorAll("input")[1].value);
        postData.append("request_regex[]",tbl1.rows[i].querySelectorAll("input")[2].value);
        postData.append("request_must[]",tbl1.rows[i].querySelector("select").value);
    }
    for(var i=1;i<tbl2.rows.length;i++){
        if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[0].value)){
            msgbox.Error("第"+i+"行，参数格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[1].value)){
            msgbox.Error("第"+i+"行，中文描述格式错误");
        return;
        }
        // if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[2].value)){
        //     msgbox.Error("第"+i+"行，正则格式错误");
        // return;
        // }
        postData.append("response_en[]",tbl2.rows[i].querySelectorAll("input")[0].value);
        postData.append("response_cn[]",tbl2.rows[i].querySelectorAll("input")[1].value);
        postData.append("response_regex[]",tbl2.rows[i].querySelectorAll("input")[2].value);
        postData.append("response_must[]",tbl2.rows[i].querySelector("select").value);
    }
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/interfacer.php?p=add",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            interface.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}
interface_edit.Show=function(item){
    interface_edit.data=item;
    msgbox.Loading();
    m.request({
        method:"GET",
        url:"php/dictionary.php?p=list"
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            interface_edit.dictionary=res.list;
            m.render(document.querySelector(".content"),m(interface_edit));
        }else{
            msgbox.Error(res.message);
        }
    })
}
interface_edit.Submit=function(){
    if(!/^.{1,300}$/.test(document.querySelector("#s_url"))){
        msgbox.Error("URL格式错误");
        return;
    }
    if(!/^.{1,300}$/.test(document.querySelector("#s_name"))){
        msgbox.Error("接口名称格式错误");
        return;
    }
    var postData=new FormData();
    postData.append("id",interface_edit.data.id);
    postData.append("s_method",document.querySelector("#s_method").value);
    postData.append("s_name",document.querySelector("#s_name").value);
    postData.append("s_url",document.querySelector("#s_url").value);
    postData.append("s_project_path",index.rootpath+system.data.s_project_path);
    var tbl1=document.querySelector(".page_content").querySelectorAll("table")[0];
    var tbl2=document.querySelector(".page_content").querySelectorAll("table")[1];
    for(var i=1;i<tbl1.rows.length;i++){
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[0].value)){
            msgbox.Error("第"+i+"行，参数格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[1].value)){
            msgbox.Error("第"+i+"行，中文描述格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl1.rows[i].querySelectorAll("input")[2].value)){
            msgbox.Error("第"+i+"行，正则格式错误");
        return;
        }
        postData.append("request_en[]",tbl1.rows[i].querySelectorAll("input")[0].value);
        postData.append("request_cn[]",tbl1.rows[i].querySelectorAll("input")[1].value);
        postData.append("request_regex[]",tbl1.rows[i].querySelectorAll("input")[2].value);
        postData.append("request_must[]",tbl1.rows[i].querySelector("select").value);
    }
    for(var i=1;i<tbl2.rows.length;i++){
        if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[0].value)){
            msgbox.Error("第"+i+"行，参数格式错误");
        return;
        }
        if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[1].value)){
            msgbox.Error("第"+i+"行，中文描述格式错误");
        return;
        }
        // if(!/^.{1,300}$/.test(tbl2.rows[i].querySelectorAll("input")[2].value)){
        //     msgbox.Error("第"+i+"行，正则格式错误");
        // return;
        // }
        postData.append("response_en[]",tbl2.rows[i].querySelectorAll("input")[0].value);
        postData.append("response_cn[]",tbl2.rows[i].querySelectorAll("input")[1].value);
        postData.append("response_regex[]",tbl2.rows[i].querySelectorAll("input")[2].value);
        postData.append("response_must[]",tbl2.rows[i].querySelector("select").value);
    }
    msgbox.Loading();
    m.request({
        method:"POST",
        url:"php/interfacer.php?p=edit",
        body:postData
    }).then(function(res){
        msgbox.Hide();
        if(res.res=="OK"){
            interface.Show();
        }else{
            msgbox.Error(res.message);
        }
    })
}

//UI
interface.view=function(){
    return [
        m("div.page_title","接口"),
        m("div.page_menu",[
            m("button",{onclick:interface_add.Show},"新增"),
            m("button",{onclick:interface.Print},"打印"),
            m("button",{onclick:interface.Save},"写入验证"),
            m("button",{onclick:interface.SaveCode},"创建代码"),
        ]),
        m("div.page_content.page_content2",m("div.pdfs",interface.data.list.map((item,index)=>{
            return m("table.pdftable",[
                m("caption",[
                    m("a",{href:"#",onclick:function(){
                        interface_edit.Show(item);
                    }},(index+1)+"."+item.s_name)
                ]),
                m("tr",[
                    m("th","请求类型"),
                    m("td",item.s_method)
                ]),
                m("tr",[
                    m("th","URL"),
                    m("td",item.s_url)
                ]),
                m("tr",[
                    m("th","REQUEST"),
                    m("td",m("table",[
                        m("tr",[
                            m("th","参数"),
                            m("th","中文描述"),
                            m("th","正则验证"),
                            m("th","是否必须")
                        ]),
                        item.request.map(r=>{
                            return m("tr",[
                                m("td",r.s_en),
                                m("td",r.s_cn),
                                m("td",r.s_regex),
                                m("td",r.s_must)
                            ])
                        })
                    ]))
                ]),
                m("tr",[
                    m("th","RESPONSE"),
                    m("td",m("table",[
                        m("tr",[
                            m("th","参数"),
                            m("th","中文描述"),
                            m("th","正则验证"),
                            m("th","是否必须")
                        ]),
                        item.response.map(r=>{
                            return m("tr",[
                                m("td",r.s_en),
                                m("td",r.s_cn),
                                m("td",r.s_regex),
                                m("td",r.s_must)
                            ])
                        })
                    ]))
                ])
            ])
        })))
    ]
}
interface_add.view=function(){
    return [
        m("div.page_title","【新增】接口"),
        m("div.page_menu",[
            m("button",{onclick:interface.Show},"返回")
        ]),
        m("div.page_content",[
            m("div.page_line",[
                m("div.page_tag","请求类型"),
                m("select.page_input#s_method",[
                    m("option",{value:"GET"},"GET"),
                    m("option",{value:"POST"},"POST")
                ])
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","* 接口名称"),
                m("input[type=text].page_input#s_name")
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","* URL"),
                m("input[type=text].page_input#s_url")
            ]),
            m("div.page_line_tag","请求参数"),
            m("table",[
                m("tr",[
                    m("th","参数"),
                    m("th","中文描述"),
                    m("th","正则"),
                    m("th","是否必须"),
                    m("th",["操作",m("button",{onclick:interface_add.AddRow},"新增行")])
                ])
            ]),
            m("div.page_line_tag","响应数据"),
            m("table",[
                m("tr",[
                    m("th","参数"),
                    m("th","中文描述"),
                    m("th","正则"),
                    m("th","是否必须"),
                    m("th",["操作",m("button",{onclick:interface_add.AddRow},"新增行")])
                ])
            ])
        ]),
        m("div.page_foot",[
            m("button",{onclick:interface_add.Submit},"保存")
        ]),
        m("datalist#dic",interface_add.dictionary.map(item=>{
            return m("option",{value:item.id},item.s_en)
        }))
    ]
}
interface_edit.view=function(){
    return [
        m("div.page_title","【更新】接口"),
        m("div.page_menu",[
            m("button",{onclick:interface.Show},"返回")
        ]),
        m("div.page_content",[
            m("div.page_line",[
                m("div.page_tag","请求类型"),
                m("select.page_input#s_method",[
                    m("option",{value:"GET",selected:interface_edit.data.s_method=="GET"?true:false},"GET"),
                    m("option",{value:"POST",selected:interface_edit.data.s_method=="POST"?true:false},"POST")
                ])
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","* 接口名称"),
                m("input[type=text].page_input#s_name",{value:interface_edit.data.s_name})
            ]),
            m("div.page_line_br"),
            m("div.page_line",[
                m("div.page_tag","* URL"),
                m("input[type=text].page_input#s_url",{value:interface_edit.data.s_url})
            ]),
            m("div.page_line_tag","请求参数"),
            m("table",[
                m("tr",[
                    m("th","参数"),
                    m("th","中文描述"),
                    m("th","正则"),
                    m("th","是否必须"),
                    m("th",["操作",m("button",{onclick:interface_add.AddRow},"新增行")])
                ]),
                interface_edit.data.request.map(r=>{
                    return m("tr",[
                        m("td",m("input[type=text]",{list:"dic",value:r.s_en,onblur:function(){
                            interface_add.GetDictionary(this)
                        }})),
                        m("td",m("input[type=text]",{value:r.s_cn})),
                        m("td",m("input[type=text]",{value:r.s_regex})),
                        m("td",m("select",[
                            m("option",{value:"是",selected:r.s_must=="是"?true:false},"是"),
                            m("option",{value:"否",selected:r.s_must=="否"?true:false},"否")
                        ])),
                        m("td",m("button",{
                            onclick:function(){
                                interface_add.DelRow(this);
                            }
                        },"删除"))
                    ])
                })
            ]),
            m("div.page_line_tag","响应数据"),
            m("table",[
                m("tr",[
                    m("th","参数"),
                    m("th","中文描述"),
                    m("th","正则"),
                    m("th","是否必须"),
                    m("th",["操作",m("button",{onclick:interface_add.AddRow},"新增行")])
                ]),
                interface_edit.data.response.map(r=>{
                    return m("tr",[
                        m("td",m("input[type=text]",{list:"dic",value:r.s_en,onblur:function(){
                            interface_add.GetDictionary(this)
                        }})),
                        m("td",m("input[type=text]",{value:r.s_cn})),
                        m("td",m("input[type=text]",{value:r.s_regex})),
                        m("td",m("select",[
                            m("option",{value:"是",selected:r.s_must=="是"?true:false},"是"),
                            m("option",{value:"否",selected:r.s_must=="否"?true:false},"否")
                        ])),
                        m("td",m("button",{
                            onclick:function(){
                                interface_add.DelRow(this);
                            }
                        },"删除"))
                    ])
                })
            ])
        ]),
        m("div.page_foot",[
            m("button",{onclick:interface_edit.Submit},"保存")
        ]),
        m("datalist#dic",interface_edit.dictionary.map(item=>{
            return m("option",{value:item.id},item.s_en)
        }))
    ]
}

